<?php

class cases extends cms_controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('cases_model', 'cases');
        $this->load->model('case_replay_model', 'replay');
    }

    public function index($type = false, $limit = FALSE, $order = FALSE) {
        if (in_array($type, array('store', 'own'))) {
            $this->pass['type'] = $type;
            switch ($type) {
                case 'store':
                    $this->load->model('store_model');
                    $store = $this->store_model->is_found(array('owner_id' => $this->user_id), array(), 'row', __CLASS__ . '/index/own', 'message_create_yourstore');
                    $where = array('store_id' => $store->id);
                    break;
                case 'own':
                    $where = array('user_id' => $this->user_id);
                    break;
                default :
                    redirect(__CLASS__ . '/index/own');
                    break;
            }
            $this->pass['result'] = $this->cases->get($where);
            $this->themes->render('cases/index', $this->pass);
        } else {
            redirect(__CLASS__ . '/index/own');
        }
    }

    public function add($id = false, $inv = false) {
        if (is_numeric($id) && $inv) {
            $case = $this->cases->get(array('item_id' => $id, 'user_id' => $this->user_id), FALSE, 'row');
            if(count($case) > 0){
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'error', 'message' => 'message_case_prev'));
            }
            $this->load->model('invoicies_model');
            $invoice = $this->invoicies_model->is_found(array('inv_num' => $inv, 'user_id' => $this->user_id), array(), 'row', '');
            $this->load->model('invoicie_items_model');
            $item = $this->invoicie_items_model->is_found(array('id' => $id, 'inv_num' => $inv), array(), 'row', '');
            $this->load->model('products_model', 'product');
            $this->pass['product'] = $product = $this->product->is_found(array('id' => $item->product_id), array(), 'row', '');

            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());
                $this->load->library('form_validation');
                $this->form_validation->set_rules('reson', lang('label_reason'), 'required|trim');
                if ($this->form_validation->run()) {
                    $save['reson'] = $this->input->post('reson');
                    $save['product_id'] = $product->id;
                    $save['user_id'] = $this->user_id;
                    $save['store_id'] = $product->store_id;
                    $save['item_id'] = $id;
                    if ($this->cases->save($save)) {
                        $this->session->unset_userdata('post');
                        $this->themes->set_message(array('url' => __CLASS__ . '/index/own', 'type' => 'success', 'message' => 'message_save_success'));
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            }
            $this->themes->render(__CLASS__ . '/add', $this->pass);
        }
    }

    public function close($id = false) {
        if (is_numeric($id)) {
            $this->cases->is_found(array('id' => $id, 'user_id' => $this->user_id), array(), false, __CLASS__, 'message_cases_canont_close');
            $this->cases->save(array('close' => 'USER'), array('id' => $id, 'user_id' => $this->user_id), true);
            $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_close_success'));
        }
        redirect($this->agent->referrer());
    }

    public function add_replay($id = false) {
        if (is_numeric($id)) {
            if ($this->input->post()) {
                $this->load->library('form_validation');
                $this->form_validation->set_rules('comment', lang('label_replay'), 'trim|required');
                if ($this->form_validation->run()) {
                    $save['case_id'] = $id;
                    $save['user_id'] = $this->user_id;
                    $save['comment'] = $this->input->post('comment');
                    if ($id_replay = $this->replay->save($save)) {
                        $this->pass['replay'] = $replay = $this->replay->get(array('id' => $id_replay), false, 'row');
                        $this->load->view($this->themes->Cms_themes() . '/cases/reply', $this->pass);
                    }
                }
            }
        }
        return false;
    }

    public function view($id = false) {
        $data['result'] = $this->cases->is_found(array('id' => $id), array(), false, __CLASS__);
        $data['reply'] = $this->replay->get(array('case_id' => $id));
        $this->themes->render('cases/view', $data);
    }

}

?>